import { instantiate } from "cc"
import { Prefab } from "cc"
import { _decorator, Component, Node } from "cc"
import { UIGameBuffItem } from "./UIGameBuffItem"
import ResManager from "../framework/ResManager"
import { Tween } from "cc"
import { Vec3 } from "cc"
import { ClientEvent } from "../framework/ClientEvent"
import { view } from "cc"
import { Size, screen } from "cc"
const { ccclass, property } = _decorator

@ccclass("UIGameBuffView")
export class UIGameBuffView extends Component {
  @property(Prefab)
  preBuffItem: Prefab = null!

  @property(Node)
  nodeBottom: Node = null!

  @property(Node)
  nodeLeft: Node = null!

  @property(Node)
  nodeMask: Node = null!

  private arrNodeItemLeft: Node[] = []
  private arrNodeItemBottom: Node[] = []
  start() {
    for (let i: number = 0; i < 4; i++) {
      const node = instantiate(this.preBuffItem)
      const interval: number = 180
      node.positionX = -16
      node.positionY = (interval / 2) * 3 - interval * i
      const uIGameBuffItem: UIGameBuffItem = node.getComponent(UIGameBuffItem)
      uIGameBuffItem.indexBuff = i
      this.nodeLeft.addChild(node)
      this.arrNodeItemLeft.push(node)
    }
    for (let i: number = 0; i < 4; i++) {
      const node = instantiate(this.preBuffItem)
      const interval: number = 150
      node.positionX = -(interval / 2) * 3 + interval * i
      node.positionY = 90
      const uIGameBuffItem: UIGameBuffItem = node.getComponent(UIGameBuffItem)
      uIGameBuffItem.indexBuff = i
      this.nodeBottom.addChild(node)
      this.arrNodeItemBottom.push(node)
    }
    this.scheduleOnce(this.playAction, 3)
    this.node.alpha = 0
    new Tween(this.node).to(0.5, { alpha: 1 }).start()
    this.updateAdapter()
  }

  protected onEnable(): void {
    ClientEvent.on("onSkillUpgradeChange", this.onSkillUpgradeChange, this)
    view.on("canvas-resize", this.updateAdapter, this)
  }

  protected onDisable(): void {
    ClientEvent.off("onSkillUpgradeChange", this.onSkillUpgradeChange, this)
    view.on("canvas-resize", this.updateAdapter, this)
  }

  private onSkillUpgradeChange() {
    if (this.nodeGuideLeft) {
      Tween.stopAllByTarget(this.nodeGuideLeft)
      this.nodeGuideLeft.destroy()
      this.nodeGuideLeft = null
    }

    if (this.nodeGuideBottom) {
      Tween.stopAllByTarget(this.nodeGuideBottom)
      this.nodeGuideBottom.destroy()
      this.nodeGuideBottom = null
    }

    if (this.nodeMask.active) {
      this.nodeMask.active = false
    }
  }
  private updateAdapter() {
    let winSize = new Size(screen.windowSize.width / view["_scaleX"], screen.windowSize.height / view["_scaleY"])
    this.nodeLeft.active = winSize.width > winSize.height
    this.nodeBottom.active = winSize.width <= winSize.height
  }
  private nodeGuideLeft: Node = null
  private nodeGuideBottom: Node = null
  private indexLeft: number = 0
  private indexBottom: number = 0
  public playAction() {
    this.nodeGuideLeft = this.playGuideAction(this.nodeLeft, this.arrNodeItemLeft, this.indexLeft)
    this.nodeGuideBottom = this.playGuideAction(this.nodeBottom, this.arrNodeItemBottom, this.indexBottom)
    if (!this.isMaskAction) {
      this.isMaskAction = true
      this.nodeMask.active = true
      new Tween(this.nodeMask).to(0.5, { alpha: 0.6 }).start()
      new Tween(this.nodeMask).delay(3).to(0.3, { alpha: 0 }).start()
    }
  }

  private isMaskAction: boolean = false

  public playGuideAction(nodeParent: Node, arrayNode: Node[], index: number) {
    const prefab = ResManager.instance.getRes("prefabs/game/gameObject/guideHand", Prefab)
    let nodeHand = instantiate(prefab)
    nodeParent.addChild(nodeHand)

    const firstScale = 0.9
    new Tween(nodeHand)
      .call(() => {
        const pos1 = arrayNode[index].getPosition()
        const pos = new Vec3(pos1.x, pos1.y + 20)
        nodeHand.setPosition(pos)
        nodeHand.scaleXYZ = firstScale
        index++
        if (index >= arrayNode.length) {
          index = 0
        }
        new Tween(nodeHand).to(0.1, { alpha: 1 }).start()
      })
      .to(0.5, { scaleXYZ: 0.8 * firstScale })
      .to(0.5, { scaleXYZ: firstScale })
      .delay(0.2)
      .to(0.5, { scaleXYZ: 0.8 * firstScale })
      .to(0.5, { scaleXYZ: firstScale })
      .call(() => {
        new Tween(nodeHand).to(0.1, { alpha: 0 }).start()
      })
      .delay(0.3)
      .union()
      .repeatForever()
      .start()
    return nodeHand
  }

  tweenGuideHand(startPos: Vec3, endPos: Vec3, node: Node) {
    
    node.scaleXYZ = 1
    node.setPosition(startPos)
    new Tween(node)
      .to(0.3, { angle: 30, scaleXYZ: 0.9 })
      .to(0.3, { angle: 15, scaleXYZ: 1 })
      .to(1, { position: endPos })
      .start()
  }
}
